package com.le.basic.linked.singleDemo;

/**
 * 打印两个有序链表的公共部分
 * 【题目】 给定两个有序链表的头指针head1和head2，打印两个 链表的公共部分。
 */
public class QuestionDemo1 {
    public static void main(String[] args) {
        singleLinked a = new singleLinked();
        a.add(new SingleNode(1));
        a.add(new SingleNode(4));
        a.add(new SingleNode(5));
        a.add(new SingleNode(7));
        singleLinked b = new singleLinked();
        b.add(new SingleNode(4));
        b.add(new SingleNode(5));
        b.add(new SingleNode(9));
        printBoth(a.getHead(),b.getHead());
    }

    public static void printBoth(SingleNode head1, SingleNode head2) {
        if (head1 == null || head2 == null) {
            return;
        }
        SingleNode a = head1;
        SingleNode b = head2;
        while (a != null && b != null) {
            if (a.data == b.data){
                System.out.println(b);
                a = a.next == null ? null : a.next;
                b = b.next == null ? null : b.next;
            }else {
                a = a.next == null ? null : a.next;
            }
        }
    }
}
